import user, { UserTypes } from './modules/user';
import app, { AppTypes } from './modules/app';
import { createStore, useStore as baseUseStore, Store } from 'vuex';
import { cloneDeep, mergeWith } from 'lodash';

const noResetKeys: string[] = ['collapsed', 'winType', 'locale', 'themeColor'];
interface State { }
export interface AllState extends State {
    user: UserTypes;
    app: AppTypes;
}
const store = createStore<State>({
    state: {},
    mutations: {
        RESET_STATE(state: any) {
            mergeWith(state, cloneState, (sVal, cVal, key) => {
                if (noResetKeys.includes(key)) return sVal;//不需要重置的数据
            });
        }
    },
    modules: {
        user,
        app
    },
}) as Store<AllState>; //类型转换吧...
const cloneState = cloneDeep<AllState>(store.state);//深度克隆state数据 用于退出重置
export default store;
export function useStore() {
    return baseUseStore<AllState>();
};